package com.ruoyi.merchant.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.merchant.domain.Statistics;
import com.ruoyi.merchant.domain.dto.StatisticsDto;
import com.ruoyi.merchant.mapper.ImsStatisticsMapper;
import com.ruoyi.merchant.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements IStatisticsService {

    @Autowired
    private ImsStatisticsMapper imsStatisticsMapper;

    @Override
    @DataScope(deptAlias = "d")
    public Map<String, Double> initTotal(StatisticsDto statisticsDto) {
        Map<String, Double> map = new HashMap();
        map.put("userCount", Double.valueOf(imsStatisticsMapper.countUser()));
        map.put("orderCount", Double.valueOf(imsStatisticsMapper.countOrder()));
        return map;
    }

    @Override
    public List<Statistics> proportionOrder() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return imsStatisticsMapper.proportionOrder(loginUser.getDeptId());
    }

    @Override
    public List<Statistics> proportionShopOrder() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return imsStatisticsMapper.proportionShopOrder(loginUser.getDeptId());
    }

    @Override
    public Map<String, List<Statistics>> proportionOrderForDay() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<Statistics> statisticsList = imsStatisticsMapper.proportionOrderForDay(loginUser.getDeptId());
        return forDay(statisticsList);
    }

    @Override
    public Map<String, List<Statistics>> proportionShopOrderForDay() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<Statistics> statisticsList = imsStatisticsMapper.proportionShopOrderForDay(loginUser.getDeptId());
        return forDay(statisticsList);
    }

    /**
     * 将统计数据进行一周与类型的格式化转换
     *
     * @param statisticsList 需要日期格式化与统计分类的集合
     * @return 转换后的集合
     */
    private Map<String, List<Statistics>> forDay(List<Statistics> statisticsList) {
        Map<String, List<Statistics>> map = new HashMap<>();
        for (Statistics statistic : statisticsList) {
            List<Statistics> statistics;
            if (map.containsKey(statistic.getName())) {
                statistics = map.get(statistic.getName());
            } else {
                statistics = initForDayStatistics(statistic.getName());
            }
            switch (statistic.getDate()) {
                case "Mon":
                    statistics.set(0, statistic);
                    break;
                case "Tue":
                    statistics.set(1, statistic);
                    break;
                case "Wed":
                    statistics.set(2, statistic);
                    break;
                case "Thu":
                    statistics.set(3, statistic);
                    break;
                case "Fri":
                    statistics.set(4, statistic);
                    break;
                case "Sat":
                    statistics.set(5, statistic);
                    break;
                case "Sun":
                    statistics.set(6, statistic);
                    break;
                default:
                    throw new RuntimeException("非法格式数据");
            }
        }
        return map;
    }

    /**
     * 初始化集合
     * @param name 标题
     * @return
     */
    private List<Statistics> initForDayStatistics(String name) {
        List<Statistics> statistics = new ArrayList<>();
        Statistics statistics1 = new Statistics();
        statistics1.setName(name);
        statistics1.setDate("Mon");
        statistics.add(statistics1);

        Statistics statistics2 = new Statistics();
        statistics2.setName(name);
        statistics2.setDate("Tue");
        statistics.add(statistics2);

        Statistics statistics3 = new Statistics();
        statistics3.setName(name);
        statistics3.setDate("Wed");
        statistics.add(statistics3);

        Statistics statistics4 = new Statistics();
        statistics4.setName(name);
        statistics4.setDate("Thu");
        statistics.add(statistics4);

        Statistics statistics5 = new Statistics();
        statistics5.setName(name);
        statistics5.setDate("Fri");
        statistics.add(statistics5);

        Statistics statistics6 = new Statistics();
        statistics6.setName(name);
        statistics6.setDate("Sat");
        statistics.add(statistics6);

        Statistics statistics7 = new Statistics();
        statistics7.setName(name);
        statistics7.setDate("Sun");
        statistics.add(statistics7);

        return statistics;
    }
}
